package com.yunhe.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunhe.common.QueryPageParam;
import com.yunhe.common.Result;
import com.yunhe.entity.GoodsType;
import com.yunhe.service.GoodsTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
@RestController
@RequestMapping("/goodstype")
@Api(value = "/goodstype",description = "物品类别信息管理")
public class GoodsTypeController {
    @Autowired
    GoodsTypeService goodsTypeService;

    @PostMapping("/listPage")
    @ApiOperation("分页查询和模糊查询")
    public Result listPage(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        String name= (String) param.get("name");

        Page<GoodsType>page = new Page<>();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<GoodsType>queryWrapper=new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank("name")&&!"null".equals(name))
            queryWrapper.like(GoodsType::getName, name);

        Page<GoodsType> result = goodsTypeService.page(page, queryWrapper);
        return Result.success(result.getRecords(),result.getTotal());
    }

    @PostMapping("/save")
    @ApiOperation("保存物品类别信息")
    public Result save(@RequestBody GoodsType goodsType){
        return goodsTypeService.save(goodsType)?Result.success():Result.fail();
    }
    @PutMapping("/update")
    @ApiOperation("更新物品类别信息")
    public Result update(@RequestBody GoodsType goodsType){
        return goodsTypeService.updateById(goodsType)?Result.success():Result.fail();
    }
    @DeleteMapping("/del")
    @ApiOperation("根据类别id删除物品类别信息")
    public Result del(String id){
        return goodsTypeService.removeById(id)?Result.success():Result.fail();
    }

    @GetMapping("/list")
    public Result list(){
        return Result.success(goodsTypeService.list());
    }
}